/************************************************************
 *  * GOSwift CONFIDENTIAL
 * __________________
 * Copyright (C) 2013-2014 CWSwift Technologies. All rights reserved.
 *
 * NOTICE: All information contained herein is, and remains
 * the property of CWSwift Technologies.
 * Dissemination of this information or reproduction of this material
 * is strictly forbidden unless prior written permission is obtained
 * from GOSwift Technologies.
 * knight lu 349919133
 *
 */

#import "FirstViewController.h"
#import "MJRefresh.h"

@interface FirstViewController ()<UITableViewDelegate,UITableViewDataSource>

@end


NSString *const MJTableViewCellIdentifier = @"Cell";

/**
 *  随机数据
 */
#define MJRandomData [NSString stringWithFormat:@"随机数据---%d", arc4random_uniform(1000000)]


@implementation FirstViewController

#pragma mark - 初始化
/**
 *  数据的懒加载
 */
- (NSMutableArray *)fakeData
{
    if (!_fakeData) {
        self.fakeData = [NSMutableArray array];
        
        for (int i = 0; i<12; i++) {
            [self.fakeData addObject:MJRandomData];
        }
    }
    return _fakeData;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // 1.注册cell
    [_aTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:MJTableViewCellIdentifier];
    
    [self setupRefresh];
}

- (void) initTable
{
    _aTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, screenWidth, screenHeight - 64) style:UITableViewStylePlain];
    
    _aTableView.delegate = self;
    _aTableView.dataSource = self;
    
    [self.view addSubview:_aTableView];

}

#pragma mark 开始进入刷新状态
- (void)headerRereshing
{
  
    // 1.添加假数据
    for (int i = 0; i<5; i++) {
        [self.fakeData insertObject:MJRandomData atIndex:0];
    }
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [_aTableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [_aTableView headerEndRefreshing];
    });
}

- (void)footerRereshing
{
    // 1.添加假数据
    for (int i = 0; i<5; i++) {
        [self.fakeData addObject:MJRandomData];
    }
    
    // 2.2秒后刷新表格UI
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        // 刷新表格
        [_aTableView reloadData];
        
        // (最好在刷新表格后调用)调用endRefreshing可以结束刷新状态
        [_aTableView footerEndRefreshing];
    });
}

#pragma mark - Table view data source
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.fakeData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //static NSString *identifer = @"myCell";
    //UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifer];
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MJTableViewCellIdentifier];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MJTableViewCellIdentifier];
    }else{
        for (UIView *oldView in cell.contentView.subviews) {
            [oldView removeFromSuperview];
        }
    }
    cell.textLabel.text = self.fakeData[indexPath.row];
    return cell;
}

/**
 *  集成刷新控件
 */
- (void)setupRefresh
{
    [self initTable];
    // 1.下拉刷新(进入刷新状态就会调用self的headerRereshing)
    [_aTableView addHeaderWithTarget:self action:@selector(headerRereshing)];
#warning 自动刷新(一进入程序就下拉刷新)
    [_aTableView headerBeginRefreshing];
    
    // 2.上拉加载更多(进入刷新状态就会调用self的footerRereshing)
    [_aTableView addFooterWithTarget:self action:@selector(footerRereshing)];
    
    // 设置文字(也可以不设置,默认的文字在MJRefreshConst中修改)
    _aTableView.headerPullToRefreshText = @"下拉可以刷新了";
    _aTableView.headerReleaseToRefreshText = @"松开马上刷新了";
    _aTableView.headerRefreshingText = @"正在帮你刷新中,不客气";
    
    _aTableView.footerPullToRefreshText = @"上拉可以加载更多数据了";
    _aTableView.footerReleaseToRefreshText = @"松开马上加载更多数据了";
    _aTableView.footerRefreshingText = @"正在帮你加载中,不客气";
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
